Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix clearing the inverse relation when has_many_inversing is enabled #42729

Merged
merged 1 commit into from Jul 8, 2021

Conversation

casperisfine
Copy link
Contributor

@casperisfine casperisfine commented Jul 8, 2021

#42601 fixed clearing the inverse relation, but it didn't account for collection associations.

For these, just assigning nil isn't possible because we need the record to remove it from the collection.

So this PR introduce an explicit method for this purpose rather than reuse inversed_from(nil).

cc @mdemare @p8

with_has_many_inversing do
book = Book.create!
citation = book.citations.create!
citation.book = nil
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this missing an assertion?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not really, this hard crash on main. But yes, I could add some assertions.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think an assertion that the inverse is cleared would be nice, but this PR looks good to me.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually that's not something that was ever supported as far as I can tell, and it seems very hard to support.

I think the fix is just to not do anything on has_many.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That makes sense...

@casperisfine casperisfine force-pushed the fix-has-many-inversing-remove branch from ce0cde1 to 2e8f2f7 Compare July 8, 2021 09:23
rails#42601 fixed clearing the inverse relation,
but it didn't account for collection associations.

For these, just assigning `nil` isn't possible because we need the record to
remove it from the collection.

So this PR introduce an explicit method for this purpose rather than
reuse `inversed_from(nil)`.
@casperisfine casperisfine force-pushed the fix-has-many-inversing-remove branch from 2e8f2f7 to 6d7235d Compare July 8, 2021 09:25
@byroot
Copy link
Member

byroot commented Jul 8, 2021

The sqlite3_mem suite failed for a random reason, other than that it's green.

@byroot byroot merged commit 317547e into rails:main Jul 8, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants